import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

//自己去创建一个线程池，用自己创建的线程池里面的线程，去执行submit提交的任务
class MyThreadPool{
    private BlockingQueue<Runnable> queue=new LinkedBlockingQueue<>();
    public MyThreadPool(int n){
        for (int i = 0; i < n; i++) {
            Thread t=new Thread(()->{
                while (true){
                    try {
                       Runnable task = queue.take();
                        task.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
//            t.setDaemon(true); //将线程设置为后台线程
            t.start();
        }
    }
    public void submit(Runnable task) throws InterruptedException {
        queue.put(task);
    }
}

public class Demo29 {
    public static void main(String[] args) throws InterruptedException {
          MyThreadPool pool=new MyThreadPool(4);
        for (int i = 0; i < 1000; i++) {
            final int id=i;
            pool.submit(()->{
                Thread cur=Thread.currentThread();
                System.out.println("hello"+cur.getName()+","+id);
            });
        }
    }
}
